﻿// -----------------------------------------------------------------------
// <copyright file="MessageOuptAdapter.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Sripirom.ATS.Core.Messaging
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using Sripirom.ATS.Core.Configuration;
    using Sripirom.ATS.Core.ExceptionHandler;
    using Sripirom.ATS.Core.Logging;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class MessageOutputdapter : IMessageOutput
    {
    
        public MessageOutput CreateOutputException(string errorDetail, string errorCode, decimal systemId)
        {
            return CreateOutputException(errorDetail, errorCode, systemId, null);
        }

        public MessageOutput CreateOutputException(string errorDetail, string errorCode, decimal systemId, 
            Dictionary<int, string> dictParam = null)
        {
            LoggerFactory.CreateLog().LogInfo("I:GetMessageDesForChannel| Message Code : " + errorCode + ", System Id : " + systemId);

            //For Channel
            MessageOutput messageOutput = new MessageOutput();

            try
            {
//                using (OracleConnection conn = new OracleConnection(AppSettingsFactory.ConfigNames().ConnectionString))
//                {
//                    String sql = String.Format(@"select MESSAGE_CODE, MESSAGE_DESCRIPTION 
//                                    from MAS_MESSAGE where SYSMESSAGE_ID =(
//                                      select SYSMESSAGE_ID from MAS_MESSAGE_MAP where message_code='{0}' and syssystem_id={1})", errorCode, systemId);
//                    OracleCommand cmd = new OracleCommand(sql, conn);
//                    OracleDataAdapter da = new OracleDataAdapter(cmd);
//                    DataTable dt = new DataTable();
//                    da.Fill(dt);
//                    if (dt != null && dt.Rows.Count > 0)
//                    {
//                        messageOutput.RESULT = dt.Rows[0]["MESSAGE_CODE"].ToString();
//                        messageOutput.ERROR_REASON = dt.Rows[0]["MESSAGE_DESCRIPTION"].ToString();
//                        if (dictParam != null)
//                        {
//                            foreach (var key in dictParam.Keys)
//                            {
//                                string strReplaceKey = '[' + key.ToString() + ']';
//                                string strReplaceValue = dictParam[key];

//                                messageOutput.ERROR_REASON = messageOutput.ERROR_REASON.Replace(strReplaceKey, strReplaceValue);
//                            }
//                        }
//                    }
//                    else
//                    {
//                        messageOutput.RESULT = Errors.UNHANDLED_EXCEPTION; ;
//                        messageOutput.ERROR_REASON = "Unhandled exception";
//                    }


//                }
            }
            catch (Exception ex)
            {
                LoggerFactory.CreateLog().LogError(ex.Message, ex);
                messageOutput.Result = Errors.UNHANDLED_EXCEPTION; ;
                messageOutput.ErrorReason = "Unhandled exception";
            }

            return messageOutput;
        }
    }
}
